#include <iostream>

using namespace std;

// FIXME: Struct or class name is better to be `Date`
struct Date
{
    int year,month,day;
};

int fw(struct Date a,int day);
int fb(struct Date a,struct Date b);
int fc(struct Date a,struct Date b);
struct Date fd(struct Date a,int delay);


int main ( )
{
    struct Date a,b,c;
    int day,w,delay;

    cout<<"enter the date"<<endl;
    cin>>a.year>>a.month>>a.day;
    cout<<"enter the date"<<endl;
    cin>>b.year>>b.month>>b.day;

    cout<<"enter the day of first day of the year"<<endl;
    cin>>day;


    w=fw(a,day);
    cout<<"this day is the "<<w<<"day of week"<<endl;

    w=fb(a,b);
    if(w>0)    cout<<"the first day is late day"<<endl;
    else if(w==0) cout<<"two dates are same"<<endl;
    else cout<<"the second day is late day"<<endl;

    w=fc(a,b);
    cout<<"the days between the date is"<< w<<endl;

    cout<<"enter the delay days"<<endl;
    cin>>delay;

    c=fd(a,delay);
    cout<<c.year<<" "<<c.month<<" "<<c.day<<endl;

    cin>>day;
    return 0;
}

int fw (struct Date a,int day)
{
    int n=0,i;
    for(i=0;i<a.month;i++)
    {
        switch(i)
        {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
        {
            n=n+31;
            break;
        }

        case 2:
        {
            n=n+28;
            if(((a.year%4==0)&&(a.year%100!=0))||(a.year%400==0)) n=n+1;
            break;
        }

        default:  n=n+30;

        }
    }

    n=n+a.day+day;

    n=n%7;
    if(n==0) n=7;

    return n;
}

int fb (struct Date a,struct Date b)
{
    if(a.year>b.year) return 1;
    else if(a.year<b.year) return -1;
    else if(a.month>b.month) return 1;
    else if(a.month<b.month) return -1;
    else if(a.day>b.day) return 1;
    else if(a.day<b.day) return -1;
    else  return 0;
}

int fc (struct Date a,struct Date b)
{
    int i,dn=0;
    struct Date c;

    if((a.year<b.year)||((a.year==b.year)&&(a.month<b.month))||((a.year=b.year)&&(a.month==b.month)&&(a.day<b.day)))
    {
        c=a;
        a=b;
        b=c;
    }
    for(i=a.year+1;i<b.year;i++)
    {
        dn=dn+365;
        if(((i%4==0)&&(i%100!=0))||(i%400==0)) dn=dn+1;
    }

    for(i=12;i>b.month;i--)
    {
        switch(i)
        {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
        {
            dn=dn+31;
            break;
        }

        case 2:
        {
            dn=dn+28;
            if(((a.year%4==0)&&(a.year%100!=0))||(a.year%400==0)) dn=dn+1;
            break;
        }

        default:  dn=dn+30;

        }
    }

    for(i=1;i<b.month;i++)
    {
        switch(i)
        {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
        {
            dn=dn+31;
            break;
        }

        case 2:
        {
            dn=dn+28;
            if(((a.year%4==0)&&(a.year%100!=0))||(a.year%400==0)) dn=dn+1;
            break;
        }

        default:  dn=dn+30;

        }
    }

    switch(a.month)
    {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
    {
        dn=dn+31-a.day;
        break;
    }

    case 2:
    {
        dn=dn+28-a.day;
        if(((a.year%4==0)&&(a.year%100!=0))||(a.year%400==0)) dn=dn+1;
        break;
    }

    default:  dn=dn+30-a.day;
    }

    dn=dn+b.day;
    return dn;
}

// FIXME: `fd` is not easy to understand, try to think a better name
struct Date fd(struct Date a,int delay)
{
    int n=0,i,md;

    /*---------------------------------calculate the  day ramaining of this year----------------------------*/
    for(i=a.month+1;i<=12;i++)
    {
        switch(i)
        {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
        {
            n=n+31;
            md=31;
            break;
        }

        case 2:
        {
            n=n+28;
            md=28;
            if(((a.year%4==0)&&(a.year%100!=0))||(a.year%400==0)) n=n+1;md++;
            break;
        }

        default:  n=n+30;md=30;

        }
    }

    switch(a.month)
    {
    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
    {
        n=n+31-a.day;
        break;
    }

    case 2:
    {
        n=n+28-a.day;
        md=28;
        if(((a.year%4==0)&&(a.year%100!=0))||(a.year%400==0))
        {
            n=n+1;
        }
        break;
    }

    default:  {
        n=n+30-a.day;
    }

    }

    /*---------------judge the delay days weather over the years-------------------------*/
    if(delay>n)
    {
        delay=delay-n;
        a.year++;

        while(delay>365)
        {
            delay=delay-365;
            if(((a.year%4==0)&&(a.year%100!=0))||(a.year%400==0))
            {
                delay--;
            }
            a.year++;
        }
    }

    /*-------------------------calculate the month of delay---------------------------------*/
    if(delay+a.day<md) a.day=a.day+delay;//the delay days can't over this month;


    else                    //the delay days over this month
    {
        switch(a.month)
        {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12:
        {
            delay=delay-31+a.day;
            break;
        }

        case 2:
        {
            delay=delay-28+a.day;
            if(((a.year%4==0)&&(a.year%100!=0))||(a.year%400==0))
            {
                delay=delay-1;
                break;
            }
        }

        default:
        {
            delay=delay-30+a.day;
        }
        }
        a.month++;



        while(delay>31)
        {
            switch(a.month)
            {
            case 1: case 3: case 5: case 7: case 8: case 10:
            {
                delay=delay-31;
                break;
            }

            case 2:
            {
                delay=delay-28;
                if(((a.year%4==0)&&(a.year%100!=0))||(a.year%400==0))
                {
                    delay--;
                }
                break;
            }

            default:  delay=delay-30;

            }
            a.month++;
        }



        switch(a.month)
        {
        case 1: case 3: case 5: case 7: case 8: case 10: case 12: md=31;break;

        case 2:
        {
            md=28;
            if(((a.year%4==0)&&(a.year%100!=0))||(a.year%400==0)) md=md+1;
            break;
        }

        default:  md=30;
        }

        if(delay>md)
        {
            a.month++;
            a.day=delay-md;
        }
        else a.day=delay;

    }

    return a;
}


